package cn.springcloudandalibaba.service.impl;

import cn.springcloudandalibaba.dao.MallOrder;
import cn.springcloudandalibaba.dao.Product;
import cn.springcloudandalibaba.dao.User;
import cn.springcloudandalibaba.exception.Asserts;
import cn.springcloudandalibaba.service.UserService;
import cn.springcloudandalibaba.service.feign.OrderService;
import cn.springcloudandalibaba.service.feign.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.springcloudandalibaba.mapper.UserMapper;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;

    @Resource
    private  UserMapper userMapper;

    @Transactional
    public int CreateOrder(int userId, int productId) {
        MallOrder mallOrder = new MallOrder();
        Product product = productService.SelectProduct(productId);
        if (product == null) {
            Asserts.fail("未找到相关的商品");
        }
        mallOrder.setProductId(productId);
        mallOrder.setUserId(userId);
        int orderId = orderService.InsertOrder(mallOrder);
        productService.UpdateProduct(product);
        return orderId;
    }

    public User GetUser(int userId){
       return userMapper.selectById(userId);
    }
    public Product GetProduct(int id){
        return productService.SelectProduct(id);
    }
}
